package com.sz.app.oss.api.common.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.base.service.ExchangeRateService;
import com.sz.biz.common.base.dto.ExchangeRateDto;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Function: ExchangeRateController <br>
 * Author: xutao <br>
 * Date: 2016-11-24 10:19:00
 */
@RestController
@RequestMapping("/api/v1/biz/exchangerate")
@Api(description = " ", tags = "024、汇率管理")
public class ExchangeRateController extends BaseController {
    @Autowired
    private ExchangeRateService exchangeRateService;

    /**
     * 新增货币汇率
     *
     * @param exchangeRateDto 要保存的数据
     * @return 操作状态
     * @throws Exception 异常信息
     */
    @ApiOperation(value = "新增汇率", notes = "增加货币之间的汇率")
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CURRENCY_RATE)
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public ResultDto addExchangeRate(@ApiParam(value = "汇率对象", required = true)
                                     @RequestBody ExchangeRateDto exchangeRateDto) {

        exchangeRateService.addCurrencyRoe(exchangeRateDto);

        return getSaveSuccessDto(MessagesCodes.MODULE_EXCHANGE_RATE);

    }

    /**
     * 修改货币汇率
     *
     * @param exchangeRateDto 要保存的数据
     * @return 操作状态
     * @throws Exception 异常信息
     */
    @ApiOperation(value = "修改汇率", notes = "修改货币之间的汇率")
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CURRENCY_RATE)
    @RequestMapping(value = "", method = {RequestMethod.PUT})
    public ResultDto updateExchangeRate(@ApiParam(value = "汇率对象", required = true)
                                        @RequestBody ExchangeRateDto exchangeRateDto) {

        exchangeRateService.updateCurrencyRoe(exchangeRateDto);


        return getUpdateSuccessDto(MessagesCodes.MODULE_EXCHANGE_RATE);
    }

    /**
     * 查询指定汇率
     *
     * @param sourceCurrency:本币代码,destCurrency：辅币代码
     * @throws Exception
     */
    @ApiOperation(value = "查询汇率", notes = "查询货币之间的汇率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sourceCurrency", value = "原始货币代码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "destCurrency", value = "目标货币代码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "过滤关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序类型,true为升序，false为降序", required = true, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "sortName", value = "排序的字段名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言信息", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/exchangeratelist", method = {RequestMethod.GET})
    public QResultDto getExchangeRate(@RequestParam(required = false) String sourceCurrency,
                                      @RequestParam(required = false) String destCurrency,
                                      @RequestParam(required = false) String beginTime,
                                      @RequestParam(required = false) String endTime,
                                      @RequestParam(required = false, defaultValue = "") String q,
                                      @RequestParam int pageIndex,
                                      @RequestParam int pageSize,
                                      @RequestParam(required = false) String sortName,
                                      @RequestParam(required = false) boolean isAsc,
                                      @RequestParam(required = false) String language) {

        q = StringUtils.replaceSqlPattern(q);
        sourceCurrency = StringUtils.replaceSqlPattern(sourceCurrency);
        destCurrency = StringUtils.replaceSqlPattern(destCurrency);
        beginTime = StringUtils.replaceSqlPattern(beginTime);
        endTime = StringUtils.replaceSqlPattern(endTime);
        language = StringUtils.replaceSqlPattern(language);

        Date beginDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (beginTime != null && !"".equalsIgnoreCase(beginTime)) {
            try {
                beginDate = sdf.parse(beginTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if (endTime != null && !"".equalsIgnoreCase(endTime)) {
            try {
                endDate = sdf.parse(endTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        QResultDto qResultDto = exchangeRateService.findCurrencyRoe(sourceCurrency,
                destCurrency, beginDate, endDate, q, pageIndex, pageSize, sortName, isAsc, language);

        return qResultDto;
    }

    /**
     * 删除汇率
     *
     * @param listId
     * @throws Exception
     */
    @ApiOperation(value = "删除汇率", notes = "删除货币之间的汇率")
    @ApiImplicitParam(name = "listId", value = "汇率ID", required = true, dataType = "List", paramType = "body")
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CURRENCY_RATE)
    @RequestMapping(value = "deletions", method = {RequestMethod.POST})
    public ResultDto deleteExchangeRate(@RequestBody List<Integer> listId) {

        exchangeRateService.batchDeleteExchangeRate(listId);

        return getDeleteSuccessDto(MessagesCodes.MODULE_EXCHANGE_RATE);
    }

    /**
     * 获取最新汇率列表
     *
     * @param sourceCurrency:本币代码,destCurrency：辅币代码
     * @throws Exception
     */
    @ApiOperation(value = "获取最新汇率列表", notes = "获取最新汇率列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sourceCurrency", value = "原始货币代码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "destCurrency", value = "目标货币代码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "过滤关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序类型,true为升序，false为降序", required = true, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "sortName", value = "排序的字段名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言信息", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/latestexchangeratelist", method = {RequestMethod.GET})
    public QResultDto getLatestExchangeRate(@RequestParam(required = false) String sourceCurrency,
                                            @RequestParam(required = false) String destCurrency,
                                            @RequestParam(required = false, defaultValue = "") String q,
                                            @RequestParam int pageIndex,
                                            @RequestParam int pageSize,
                                            @RequestParam(required = false) String sortName,
                                            @RequestParam(required = false) boolean isAsc,
                                            @RequestParam(required = false) String language) {

        q = StringUtils.replaceSqlPattern(q);
        sourceCurrency = StringUtils.replaceSqlPattern(sourceCurrency);
        destCurrency = StringUtils.replaceSqlPattern(destCurrency);
        language = StringUtils.replaceSqlPattern(language);

        QResultDto qResultDto = exchangeRateService.getLatestExchangeRateList(sourceCurrency,
                destCurrency, q, pageIndex, pageSize, sortName, isAsc, language);

        return qResultDto;
    }
}
